package com.yuncheng.spcyApi.spcy;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.bo.AddJcyParamBo;
import com.yuncheng.spcyApi.bo.CommonIdsBo;
import com.yuncheng.spcyApi.bo.ParamsBo;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.spcyService.SpcyJcyService;
import com.yuncheng.spcyApi.entity.SpcyLog;
import com.yuncheng.spcyApi.entity.SpcyRsHcy;
import com.yuncheng.spcyApi.entity.SpcyRsJcyqjgl;
import com.yuncheng.spcyApi.service.ISpcyRsJcyqjglService;
import com.yuncheng.spcyApi.vo.common.OperationLogVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.jcy.RsHcyVo;
import com.yuncheng.spcyApi.vo.spcy.SpcyFlowVo;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 审评查验-检查员管理
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Api(tags = "审评查验-检查员管理接口")
@RestController
@RequestMapping("/api/spzx-spcy/jcygl")
public class SpcyJcyController {
    private static final Logger log = LoggerFactory.getLogger(SpcyJcyController.class);

    @Resource
    private ISpcyRsJcyqjglService spcyRsJcyqjglService;

    @Resource
    private SpcyJcyService spcyJcyService;


    @Log(title = "检查员类别管理总库-分页列表查询")
    @ApiOperation(value = "检查员类别管理总库-分页列表查询")
    @GetMapping(value = "/queryPageList")
    public HttpResult<?> queryPageList(RsHcyVo spcyRsHcy,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "100") Integer pageSize,
                                       @RequestParam(value = "orderItem",defaultValue = "")String orderItem,
                                       @RequestParam(value = "orderType",defaultValue = "asc")String orderType) {
        Page<RsHcyVo> page = new Page<>(pageNo, pageSize);
        return spcyJcyService.queryPageList(page,spcyRsHcy,orderItem,orderType);
    }

    @Log(title = "检查员账户管理-分页列表查询")
    @ApiOperation(value = "检查员账户管理-分页列表查询")
    @GetMapping(value = "/pageJcyAccountList")
    public HttpResult<?> pageJcyAccountList(RsHcyVo user,
                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                            @RequestParam(name = "pageSize", defaultValue = "100") Integer pageSize) {
        Page<RsHcyVo> page = new Page<>(pageNo, pageSize);
        user.setFstate(SpcyCommonConstant.JCY_STATE_YX);
        return spcyJcyService.pageJcyAccountList(page,user);
    }

    @Log(title = "检查员查询统计管理-分页列表查询")
    @ApiOperation(value = "检查员查询统计管理-分页列表查询")
    @GetMapping(value = "/jcyCxtjPageList")
    public HttpResult<?> jcyCxtjPageList(RsHcyVo user,
                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "100") Integer pageSize) {
        Page<RsHcyVo> page = new Page<>(pageNo, pageSize);
        user.setFstate(SpcyCommonConstant.JCY_STATE_YX);
        return spcyJcyService.jcyCxtjPageList(page,user);
    }

    @Log(title = "查询检查员所有待办列表")
    @ApiOperation(value = "查询检查员所有待办列表")
    @GetMapping("queryDbPage")
    public HttpResult<?> queryDbPage(
            SpcyFlowVo spcyFlowVo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "onlyCurrentUser", defaultValue = "true") boolean onlyCurrentUser, OrderItemVo orderItemVo){

        Page<SpcyFlowVo> page = new Page<>(pageNo, pageSize);
        return spcyJcyService.queryDbPage(page,spcyFlowVo,onlyCurrentUser,orderItemVo);
    }

    @Log(title = "检查员请假列表-分页查询")
    @ApiOperation(value = "检查员请假列表-分页查询")
    @GetMapping(value = "queryQjPage")
    public HttpResult queryQjPage(SpcyRsJcyqjgl jcyqjgl,
                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        Page<SpcyRsJcyqjgl> page = new Page<>(pageNo,pageSize);

        LambdaQueryWrapper<SpcyRsJcyqjgl> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(jcyqjgl.getFxm()), SpcyRsJcyqjgl::getFxm, jcyqjgl.getFxm());
        if (jcyqjgl.getFqjkssj() != null){
            wrapper.ge(SpcyRsJcyqjgl::getFqjkssj, jcyqjgl.getFqjkssj());
        }
        if (jcyqjgl.getFqjjssj() != null){
            wrapper.le(SpcyRsJcyqjgl::getFqjjssj, jcyqjgl.getFqjjssj());
        }
        wrapper.orderByDesc(SpcyRsJcyqjgl::getCreateTime);
        IPage<SpcyRsJcyqjgl> pageList = spcyRsJcyqjglService.page(page, wrapper);
        return HttpResult.ok(pageList);
    }

    @Log(title = "新增检查员请假")
    @ApiOperation(value = "新增检查员请假")
    @PostMapping(value = "addQj")
    public HttpResult addQj(@RequestBody SpcyRsJcyqjgl jcyqjgl){
        SpcyRsJcyqjgl qj = spcyRsJcyqjglService.addQj(jcyqjgl);
        return HttpResult.ok(qj);
    }

    @Log(title = "更新检查员请假")
    @ApiOperation(value = "更新检查员请假")
    @PutMapping(value = "updateQj")
    public HttpResult updateQj(@RequestBody SpcyRsJcyqjgl jcyqjgl){
        spcyRsJcyqjglService.updateQj(jcyqjgl);
        return HttpResult.ok("保存成功");
    }

    @Log(title = "删除检查员请假")
    @ApiOperation(value = "删除检查员请假")
    @DeleteMapping(value = "deleteQj")
    public HttpResult deleteQj(@RequestParam(value = "id",defaultValue = "")String ids){
        spcyRsJcyqjglService.deleteQj(ids);

        return HttpResult.ok("删除成功");
    }

    @Log(title = "取消检查员请假")
    @ApiOperation(value = "取消检查员请假")
    @DeleteMapping(value = "cancelQj")
    public HttpResult cancelQj(@RequestParam(value = "id",defaultValue = "")String ids){
        spcyRsJcyqjglService.cancelQj(ids);

        return HttpResult.ok("取消成功");
    }

    @Log(title = "查询检查员请假详情-根据id")
    @ApiOperation(value = "查询检查员请假详情-根据id")
    @GetMapping(value = "queryById")
    public HttpResult queryById(@RequestParam(value = "id",defaultValue = "")String id){
        SpcyRsJcyqjgl jcyqj = spcyRsJcyqjglService.getById(id);

        return HttpResult.ok(jcyqj);
    }

    @Log(title = "查询检查员操作记录-分页查询")
    @ApiOperation(value = "查询检查员操作记录-分页查询")
    @GetMapping(value = "queryJcyglLogRecord")
    public HttpResult queryJcyglLogRecord(OperationLogVo logVo,
                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "100") Integer pageSize){
        Page<SpcyLog> page = new Page<>(pageNo,pageSize);

        return spcyJcyService.queryJcyglLogRecord(page, logVo);
    }

    @Log(title = "获取对应的账户信息-根据检查员id")
    @ApiOperation(value = "获取对应的账户信息-根据检查员id")
    @GetMapping(value = "getUserInfoByJcyId")
    public HttpResult getUserInfoByJcyId(@RequestParam(value = "id",defaultValue = "")String id){
        SpcyRsHcy jcy = spcyJcyService.getUserInfoByJcyId(id);

        return HttpResult.ok(jcy);
    }

    @Log(title = "更新检查员用户信息")
    @ApiOperation(value = "更新检查员用户信息")
    @PostMapping(value = "updateUserInfo")
    public HttpResult<?> updateJcyInfo(@RequestBody HashMap<String,String> buffer){
        spcyJcyService.updateJcyInfo(buffer);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "批量设置组长/组员")
    @ApiOperation(value = "批量设置组长/组员")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "多个id逗号拼接"),
            @ApiImplicitParam(name = "fssk",value = "组长/组员/预备检查员")
    })
    @PostMapping(value = "batchSzSsk")
    public HttpResult batchSzSsk(@RequestBody Map<String,String> paramsBo){
        spcyJcyService.batchSzSsk(paramsBo);
        return HttpResult.ok("设置成功");
    }

    @Log(title = "批量设置检查员有效状态")
    @ApiOperation(value = "批量设置检查员有效状态")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "多个id逗号拼接"),
            @ApiImplicitParam(name = "fstate",value = "有效状态")
    })
    @PostMapping(value = "batchSzJcyState")
    public HttpResult batchSzJcyState(@RequestBody Map<String,String> paramsBo){
        spcyJcyService.batchSzJcyState(paramsBo);
        return HttpResult.ok("设置成功");
    }

    @Log(title = "查看账户检查员情况")
    @ApiOperation(value = "查看账户检查员情况")
    @GetMapping(value = "seeAccount")
    public HttpResult seeAccount(@RequestParam(value = "id",defaultValue = "")String id){
        AddJcyParamBo paramBo = spcyJcyService.seeAccount(id);

        return HttpResult.ok(paramBo);
    }

    @Log(title = "更新检查员高频库状态")
    @ApiOperation(value = "更新检查员高频库状态")
    @GetMapping(value = "updateJcyGpk")
    public HttpResult updateJcyGpk(@RequestParam(value = "id",defaultValue = "") @ApiParam("检查员id") String id,
                                   @RequestParam(value = "fgpk",defaultValue = "") @ApiParam("是否为高频库(是/否)") String fgpk){
        return spcyJcyService.updateJcyGpk(id, fgpk);
    }

    @Log(title = "批量设置-检查员高频库")
    @ApiOperation(value = "批量设置-检查员高频库")
    @PostMapping(value = "setJcyGpkPl")
    public HttpResult setJcyGpkPl(@RequestBody CommonIdsBo parBo){

        return spcyJcyService.batchSetJcyGpk(parBo.getIds(), SpcyConstant.JCY_GKP_YES);
    }

    @Log(title = "批量取消设置-检查员高频库")
    @ApiOperation(value = "批量取消设置-检查员高频库")
    @PostMapping(value = "qxSetJcyGpkPl")
    public HttpResult qxSetJcyGpkPl(@RequestBody CommonIdsBo parBo){

        return spcyJcyService.batchSetJcyGpk(parBo.getIds(), SpcyConstant.JCY_GKP_NO);
    }

    @Log(title = "新增/更新账户")
    @ApiOperation(value = "新增/更新账户")
    @PostMapping(value = "addAccount")
    public HttpResult addAccount(@RequestBody AddJcyParamBo jcyParamBo){
        String userid = spcyJcyService.addAccount(jcyParamBo);

        HttpResult result = new HttpResult(true);
        result.setMessage("添加成功");
        result.setResult(userid);
        return result;
    }

    @Log(title = "停用账户")
    @ApiOperation(value = "停用账户")
    @PostMapping(value = "stopAccount")
    public HttpResult stopAccount(@RequestBody ParamsBo paramsBo){
        spcyJcyService.stopAccount(paramsBo);

        return HttpResult.ok("冻结成功");
    }

    @Log(title = "恢复账户")
    @ApiOperation(value = "恢复账户")
    @PostMapping(value = "enableAccount")
    public HttpResult enableAccount(@RequestBody ParamsBo paramsBo){
        spcyJcyService.enableAccount(paramsBo);

        return HttpResult.ok("恢复成功");
    }

    @Log(title = "账户重置密码")
    @ApiOperation(value = "账户重置密码")
    @ApiImplicitParam(name = "ids",value = "用户ids")
    @PostMapping(value = "resetPwd")
    public HttpResult<?> resetPwd(@RequestBody ParamsBo paramsBo){
        if (StringUtils.isBlank(paramsBo.getIds())){
            return HttpResult.error("请选择数据！");
        }
        spcyJcyService.resetPwd(paramsBo.getIds());

        return HttpResult.ok("重置成功");
    }

    @Log(title = "导入检查员账户数据-表格excel")
    @ApiOperation(value = "导入检查员账户数据-表格excel")
    @PostMapping(value = "impJcyFileData")
    public HttpResult<?> impJcyFileData(@RequestParam("file") MultipartFile file){

        return spcyJcyService.impJcyFileData(file);
    }

    @Log(title = "下载检查员账户-导入模板")
    @ApiOperation(value = "下载检查员账户-导入模板")
    @GetMapping("/downJcyTemplate")
    public void downJcyTemplate(HttpServletResponse response){
        spcyJcyService.downJcyTemplate(response);
    }

    /*@Log(title = "导出检查员账户数据-表格excel")
    @ApiOperation(value = "导出检查员账户数据-表格excel")
    @GetMapping("/exportJcyData")
    public void exportJcyData(RsHcyVo rsHcyVo, HttpServletResponse response){
        if (StringUtils.isNotBlank(rsHcyVo.getIds())){
            rsHcyVo.setUserids(rsHcyVo.getIds());
            rsHcyVo.setIds(null);
        }
        spcyJcyService.exportJcyData(rsHcyVo, response);
    }*/

    @Log(title = "导出检查员账户数据-表格excel")
    @ApiOperation(value = "导出检查员账户数据-表格excel")
    @GetMapping("/exportJcyData")
    public void exportJcyData(RsHcyVo rsHcyVo, HttpServletResponse response){
        /*if (StringUtils.isNotBlank(rsHcyVo.getIds())){
            rsHcyVo.setUserids(rsHcyVo.getIds());
            rsHcyVo.setIds(null);
        }*/
        rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX);
        spcyJcyService.exportJcyDataNew(rsHcyVo, null, response);
    }

    /**
     * 导出-测试所有检查员用户
     * @param rsHcyVo
     * @param response
     */
    @GetMapping("/exportJcyData2")
    public void exportJcyData2(RsHcyVo rsHcyVo, HttpServletResponse response){
        spcyJcyService.exportJcyData2(rsHcyVo, null, response);
    }

//    @PostMapping(value = "impJcyFileData2")
    public void impJcyFileData2(@RequestParam("file") MultipartFile file, HttpServletResponse response){

        spcyJcyService.impJcyFileData2(file, response);
    }

    /** 身份证号码 */
    @PostMapping(value = "impJcyFileData3")
    public void impJcyFileData3(@RequestParam("file") MultipartFile file,
                                @RequestParam(value = "fxllx",defaultValue = "")String fxllx,
                                @RequestParam(value = "fjcylb",defaultValue = "")String fjcylb,
                                @RequestParam(value = "fjcyxl",defaultValue = "")String fjcyxl,HttpServletResponse response){

        spcyJcyService.impJcyFileData3(file, fxllx, fjcylb, fjcyxl, response);
    }

    @Log(title = "导出检查员类别数据-表格excel")
    @ApiOperation(value = "导出检查员类别数据-表格excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "核查员id字符逗号分割",dataType = "String")
    })
    @GetMapping("/exportJcyLbData")
    public void exportJcyLbData(RsHcyVo rsHcyVo, HttpServletResponse response){
        spcyJcyService.exportJcyLbData(rsHcyVo, response);
    }

    @GetMapping("/exportJcyLbDataTest2")
    public void exportJcyLbDataTest2(HttpServletResponse response){
        spcyJcyService.exportJcyLbDataTest2(new YcSysUser(), null, response);
    }

    @GetMapping("/exportJcyLbDataTest")
    public void exportJcyLbDataTest(HttpServletResponse response){
        spcyJcyService.exportJcyLbDataTest(response);
    }

    @ApiOperation(value = "数据对比-工作单位-正式检查员")
    @PostMapping(value = "dataComparisonExportZsJcyInfo")
    public void dataComparisonExportZsJcyInfo(@RequestParam("file")MultipartFile file,HttpServletResponse response){
        spcyJcyService.dataComparisonExportZsJcyInfo(file,response);
    }

    @ApiOperation(value = "数据对比-手机号-正式检查员")
    @PostMapping(value = "dataComparisonPhoneExportZsJcyInfo")
    public void dataComparisonPhoneExportZsJcyInfo(@RequestParam("file")MultipartFile file,HttpServletResponse response){
        spcyJcyService.dataComparisonPhoneExportZsJcyInfo(file,response);
    }

    @ApiOperation(value = "数据对比-手机号-正式检查员")
    @PostMapping(value = "batchSetJcyGpkByFile")
    public void batchSetJcyGpkByFile(@RequestParam("file")MultipartFile file,
                                     @RequestParam(value = "fjcyxl",defaultValue = "") String fjcyxl,
                                     HttpServletResponse response){
        spcyJcyService.batchSetJcyGpkByFile(file, fjcyxl, response);
    }
}
